package net.lahuobao.mall.controller.admin;

import net.lahuobao.export.util.ExportFileUtil;
import net.lahuobao.mall.entity.admin.MallShopSettlePeriodEntity;
import net.lahuobao.mall.service.admin.MallShopSettlePeriodServiceI;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.DataGridReturn;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validator;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

/**   
 * @Title: Controller
 * @Description: 结算规则
 * @author zhangdaihao
 * @date 2016-10-09 10:40:46
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/mallShopSettlePeriodController")
public class MallShopSettlePeriodController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MallShopSettlePeriodController.class);

	@Autowired
	private MallShopSettlePeriodServiceI mallShopSettlePeriodService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	@Resource
	private ExportFileUtil exportFileUtil;

	/**
	 * 结算规则列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("net/lahuobao/mall/admin/mallShopSettlePeriodList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MallShopSettlePeriodEntity mallShopSettlePeriod, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		this.selectData(mallShopSettlePeriod, request, dataGrid);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 添加结算规则
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(MallShopSettlePeriodEntity mallShopSettlePeriod, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		BigDecimal bg = new BigDecimal(100);
		if (StringUtil.isNotEmpty(mallShopSettlePeriod.getId())) {
			message = "结算规则更新成功";
			MallShopSettlePeriodEntity t = mallShopSettlePeriodService.get(MallShopSettlePeriodEntity.class, mallShopSettlePeriod.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(mallShopSettlePeriod, t);
				//设置服务费率
				t.setSettlerate(mallShopSettlePeriod.getSettlerate().divide(bg));
				t.setSettlerateadvance(mallShopSettlePeriod.getSettlerateadvance().divide(bg));
				mallShopSettlePeriodService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "结算规则更新失败";
			}
		} else {
			/*message = "结算规则添加成功";
			mallShopSettlePeriodService.save(mallShopSettlePeriod);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);*/
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 结算规则列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(MallShopSettlePeriodEntity mallShopSettlePeriod, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(mallShopSettlePeriod.getId())) {
			mallShopSettlePeriod = mallShopSettlePeriodService.getEntity(MallShopSettlePeriodEntity.class, mallShopSettlePeriod.getId());
			req.setAttribute("mallShopSettlePeriodPage", mallShopSettlePeriod);
		}
		return new ModelAndView("net/lahuobao/mall/admin/mallShopSettlePeriod");
	}

	@RequestMapping(params = "export")
	public void middleExpenditureExport(MallShopSettlePeriodEntity mallShopSettlePeriod, HttpServletRequest request, HttpServletResponse response,
										DataGrid dataGrid){
		DataGridReturn dg = this.selectData(mallShopSettlePeriod, request, dataGrid);
		exportFileUtil.setFileName("商户结算规则", "xlsx");
		// 构造数据导出
		try {
			int page = 1;
			int rows = 1000;
			exportFileUtil.initExport(response);
			request.setAttribute("pageExport", "yes");
			request.setAttribute("page", page);
			request.setAttribute("rows", rows);
			int begin_ = exportFileUtil.exportIOStream(response, dg.getRows() , rows, page, 0);
			for(int i = 2; begin_ != page; i++ ){
				page = begin_;
				request.setAttribute("page", i);
				begin_ = exportFileUtil.exportIOStream(response, dg.getRows(), rows, begin_, 1);
			}
			exportFileUtil.finish(response);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 查询数据
	 * @param mallShopSettlePeriod
	 * @param request
	 * @param dataGrid
	 * @return
	 */
	private  DataGridReturn selectData(MallShopSettlePeriodEntity mallShopSettlePeriod, HttpServletRequest request, DataGrid dataGrid){
		CriteriaQuery cq = new CriteriaQuery(MallShopSettlePeriodEntity.class, dataGrid);
		cq.createAlias("mallShop","MallShopEntity");

		//商家名称
		if(null != mallShopSettlePeriod.getMallShop() && StringUtil.isNotEmpty(mallShopSettlePeriod.getMallShop().getCompanyname())){
			cq.like("MallShopEntity.companyname", "%"+mallShopSettlePeriod.getMallShop().getCompanyname()+"%");
			mallShopSettlePeriod.getMallShop().setCompanyname("");
		}

		//店铺名称
		if(null != mallShopSettlePeriod.getMallShop() && StringUtil.isNotEmpty(mallShopSettlePeriod.getMallShop().getShopname())){
			cq.like("MallShopEntity.shopname", "%"+mallShopSettlePeriod.getMallShop().getShopname()+"%");
			mallShopSettlePeriod.getMallShop().setShopname("");
		}

		String start = request.getParameter("mallShop.createtime_begin");
		String end = request.getParameter("mallShop.createtime_end");

		try{
			if(StringUtil.isNotEmpty(start) && StringUtil.isNotEmpty(end)){
				cq.ge("MallShopEntity.createtime", new SimpleDateFormat("yyyy-MM-dd").parse(start));
				cq.le("MallShopEntity.createtime", new SimpleDateFormat("yyyy-MM-dd").parse(end));
			}
		}catch (Exception e){
			e.printStackTrace();
		}

		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallShopSettlePeriod, request.getParameterMap());
		return  this.mallShopSettlePeriodService.getDataGridReturn(cq, true);
	}
}
